﻿using System;
using ASP;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;



public partial class SiteInfo_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
            
        }
    }

    private void bd()
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();

        DataList1.DataSource = dal.SiteType.Where(x => x.pid.Equals((object)null));
        DataList1.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities model = DAL.DBManager.GetCmsEmtity();
        //判断导航名称是否存在
        int i = model.SiteInfo.Where(x => x.SiteName == txtName.Text).ToList().Count;
        if (i != 0)
            return;

        string strUrl = string.Empty;
        if (FileUpload1.HasFile)
        {
            string fileType = FileUpload1.PostedFile.ContentType;
            if (fileType == "image/bmp" || fileType == "image/gif" || fileType == "image/pjpeg" || fileType == "image/x-png")
            {
               
                string saveUrl = @"\images\";
                string appPath = Request.PhysicalApplicationPath;
                string savePath = appPath + saveUrl +
                Server.HtmlEncode(FileUpload1.FileName);
                FileUpload1.SaveAs(savePath);
                strUrl = saveUrl + FileUpload1.FileName; ;
                //ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('文件类型不对！请选择图片！')", true);
                return;
            }
        }


        string str = hidSelectValue.Value;
      //判断是否选择了导航分类
        if (str == "")
        {
            CMSModel.SiteInfo type = new CMSModel.SiteInfo();
            type.EditTime = DateTime.Now;
            //type.sid = txtId.Text;
            type.SiteName = txtName.Text;
            type.memo = txtMemo.Text;
            type.SiteUrl = txtUrl.Text;
            type.pic = strUrl;
            if (ddlWt.SelectedItem.Text!="无")
            type.Weight = int.Parse(ddlWt.SelectedValue);
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            try
            {
                dal.AddToSiteInfo(type);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功'),document.location='Default.aspx'", true);
            }
            catch (Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "addSiteInfo", LogYuMin.LogStates.ERROR, ex);
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败')", true);
            }
        }
        else
        {
            try
            {
                CMSModel.SiteInfo type = new CMSModel.SiteInfo();
                type.EditTime = DateTime.Now;

                type.SiteName = txtName.Text;
                type.memo = txtMemo.Text;
                type.SiteUrl = txtUrl.Text;
                type.pic = strUrl;
                if (ddlWt.SelectedItem.Text != "无")
                    type.Weight = int.Parse(ddlWt.SelectedValue);

                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                dal.AddToSiteInfo(type);
                dal.SaveChanges();

               int sid= dal.SiteInfo.First(x => x.SiteName== txtName.Text).sid;
             
                foreach (var c in str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    CMSModel.SiteType_SiteInfo type1 = new CMSModel.SiteType_SiteInfo();
                    type1.EditTime = DateTime.Now;
                    type1.sid = sid;
                    type1.stid =int.Parse(c);
                    dal.AddToSiteType_SiteInfo(type1);
                    dal.SaveChanges();
                }

                
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功'),document.location='Default.aspx'", true);
            }
            catch (Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "addSiteInfo", LogYuMin.LogStates.ERROR, ex);
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败')", true);
            }
        }
    }

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var c = (Stie_TypeSelect)e.Item.FindControl("SiteTypeSelect1");
        c.BindData(0);
    }

    protected void btnPic_Click(object sender, EventArgs e)
    {
        //string strUrl = string.Empty;
        //if (FileUpload1.HasFile)
        //{
        //    string fileType = FileUpload1.PostedFile.ContentType;
        //    if (fileType == "image/bmp" || fileType == "image/gif" || fileType == "image/pjpeg" || fileType == "image/x-png")
        //    {
        //        string saveUrl = @"\images\";
        //        string appPath = Request.PhysicalApplicationPath;
        //        string savePath = appPath + saveUrl +
        //        Server.HtmlEncode(FileUpload1.FileName);
        //        FileUpload1.SaveAs(savePath);

        //        strUrl = saveUrl + FileUpload1.FileName;
        //        //ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
        //    }
        //    else
        //    {
        //        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传失败，文件类型不对！请选择图片！')", true);
        //    }
        //}
      
    }

}

